using UnityEngine;
using System.Collections;
using UnityEditor;

#if UNITY_EDITOR
/// \class  gkSequencerEditorWindow
/// \brief 	The shoot in rhythm editor window
public class gkSequencerEditorWindow : EditorWindow
{	
	/// The current selected sequencer
	private gkSequencer m_rSelectedSequencer;
	
	/// The Gui skin
	private GUISkin m_rGUISkinSequencer;
	
	[MenuItem ("Window/Sequencer")]
	/// \brief  Show the window
    static void ShowWindow() 
	{		
    	EditorWindow.GetWindow(typeof(gkSequencerEditorWindow), false, "Sequencer");
    }
	
	void Update()
	{
		Repaint();
	}
	
	/// \brief  Called on gui refresh
	void OnGUI()
	{
		if(TryToFindGUISkin())
		{
			// Set the sequencer gui skin
			GUI.skin = m_rGUISkinSequencer;
		}
		else
		{
			Debug.LogError("GUISkinSequencer doesn't found.");
		}
		
		// Update the windaw
		UpdateWindow();
    }
	
	/// \brief  Update the window
	void UpdateWindow()
	{
		// Update the sequencer selection
		UpdateSequencerSelection();
		
		// Display the sequencer GUI
		DisplaySequencerGUI();	
	}
	
	/// \brief  Update the sequencer selection
	void UpdateSequencerSelection()
	{
		// If there is a game object selected
		if(Selection.activeGameObject != null)
		{
			gkSequencer rNewSequencer;
			
			// Try to get the sequencer component
			rNewSequencer = Selection.activeGameObject.GetComponent<gkSequencer>();
			if(rNewSequencer != null)
			{
				m_rSelectedSequencer = rNewSequencer;
			}
		}
	}
	
	/// \brief  Display the sequencer gui
	void DisplaySequencerGUI()
	{
		if(m_rSelectedSequencer != null)
		{
			m_rSelectedSequencer.SendMessage("DisplaySequencerGUI", SendMessageOptions.DontRequireReceiver);
		}
	}
	
	/// \brief  Try To set the sequencer GUISkin
	bool TryToFindGUISkin()
	{
		if(m_rGUISkinSequencer == null)
		{
			m_rGUISkinSequencer = Resources.Load("GUISkinSequencer") as GUISkin;
		}
		
		return m_rGUISkinSequencer != null;
	}
}
#endif